package com.alan.controller;

import com.alan.common.RegisterConst;
import com.alan.entity.RestBean;
import com.alan.entity.dto.RegisterUserDto;
import com.alan.service.AuthenticationService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import jakarta.validation.constraints.Pattern;
import org.hibernate.validator.constraints.Length;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Validated
@RestController
@RequestMapping("/api/auth")
public class AuthController {

    private final String EMAIL_REGEX = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
    private final String USERNAME_REGEX = "^[a-zA-Z0-9\\u4e00-\\u9fa5]+$";

    @Resource
    private AuthenticationService authenticationService;


    /*发送验证邮件*/
    @PostMapping ("/valid-register-email")
    public RestBean<String> validateEmail(@Pattern(regexp = EMAIL_REGEX)
                                              @RequestParam("email") String email,
                                                HttpSession session){
        RestBean msg = authenticationService.sendValidateEmail(email,session.getId(),false);

        if (msg.getMassage().equals(RegisterConst.SEND_SUCCESS)){
            return RestBean.success("邮件已发送，请查收");
        }else if (msg.getMassage().equals(RegisterConst.ACCOUNT_EXISTED)){
            return RestBean.failure(100,"用户已存在，请你确认邮箱！");
        }else {
            return RestBean.failure(100,"发生错误，请联系管理员！");
        }
    }
    @PostMapping("/valid-reset-email")
    public RestBean validateResetEmail(@Pattern(regexp = EMAIL_REGEX) @RequestParam("email") String email,
                                       HttpSession session){
        return authenticationService.sendValidateEmail(email, session.getId(), true);
    }

    /*注册*/
    @PostMapping("/register")
//    public RestBean<String> registerUser(@RequestBody RegisterUserDto userDto){ 验证参数，防止绕后
    public RestBean<String> registerUser(@Pattern(regexp = USERNAME_REGEX)@Length(min = 2,max = 8)@RequestParam String username,
                                         @Length(min = 6,max = 16)@RequestParam String password,
                                         @Pattern(regexp = EMAIL_REGEX)@RequestParam String email,
                                         @Length(min = 4,max = 4)@RequestParam String code,
                                         HttpSession session){
        RegisterUserDto registerUserDto = new RegisterUserDto(email,username,password,code);

        return authenticationService.vaildateAndRegister(registerUserDto,session.getId());
    }

    /*重置密码*/
    @PostMapping("/start-reset")
    public RestBean<String> startReset(@Pattern(regexp = EMAIL_REGEX) @RequestParam("email") String email,
                                       @Length(min = 4, max = 4) @RequestParam("code") String code,
                                       HttpSession session) {
            RestBean restBean = authenticationService.validateOnly(email,code,session.getId());
            if (restBean.getMassage().equals("验证成功")){
                //TODO：可以交给service
                session.setAttribute("reset-password",email);
            }
            return restBean;
    }


    @PostMapping("/do-reset")
    public RestBean<String> resetPassword(@Length(min = 6, max = 16) @RequestParam("password") String password,
                                          HttpSession session){
        String email = (String) session.getAttribute("reset-password");
        if(email == null) {
            return RestBean.failure(401, "请先完成邮箱验证");
        } else if(authenticationService.resetPassword(password, email)){
            session.removeAttribute("reset-password");
            return RestBean.success("密码重置成功");
        } else {
            return RestBean.failure(500, "内部错误，请联系管理员");
        }
    }




}
